package com.nativetalk.service.promocode.update;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.FoundChange.TdNtMembFoundChangeInset;
import com.nativetalk.bean.TdNtCooperation;
import com.nativetalk.bean.TdNtCooperationMember;
import com.nativetalk.bean.member.TdNtMemberInvite;
import com.nativetalk.bean.promocode.TdNtPromoCodeUpdate;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.TdNtCooperationDao;
import com.nativetalk.dao.TdNtMembFoundChangeDao;
import com.nativetalk.dao.TdNtMemberDao;
import com.nativetalk.dao.TdNtPromoCodeDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by lizhun on 16/3/19.
 */
@Service
public class PromocodeUpdateServiceImpl implements PromocodeUpdateService {
    private static Logger logger = Logger.getLogger(PromocodeUpdateServiceImpl.class);
    @Resource
    private TdNtPromoCodeDao tdNtPromoCodeDao;
    @Resource
    private TdNtMembFoundChangeDao tdNtMembFoundChangeDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Resource
    private TdNtCooperationDao tdNtCooperationDao;

    @Override
    @Transactional
    public RetInfo receive(String promo_code, int memb_id) {
        String logInfo = this.getClass().getName() + ":receive:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             * 查找优惠码是否存在
             */
            TdNtPromoCodeUpdate tdNtPromoCodeUpdate = tdNtPromoCodeDao.findTdNtPromoCodeUpdate(promo_code);

            /**
             * 判断优惠码是否有效
             */
            if (tdNtPromoCodeUpdate != null) {
                /**
                 * 添加资金变化记录
                 */
                BigDecimal memb_balance = tdNtMemberDao.findTdNtMemberBalabce(memb_id);
                TdNtMembFoundChangeInset tdNtMembFoundChangeInset = new TdNtMembFoundChangeInset();
                tdNtMembFoundChangeInset.setMemb_id(memb_id);
                tdNtMembFoundChangeInset.setMfchg_back_amount(memb_balance.add(tdNtPromoCodeUpdate.getPromo_amount()));
                tdNtMembFoundChangeInset.setMfchg_change_amount(tdNtPromoCodeUpdate.getPromo_amount());
                tdNtMembFoundChangeInset.setMfchg_change_month(CommonUtil.getMonth(new Date()));
                tdNtMembFoundChangeInset.setMfchg_front_amount(memb_balance);
                tdNtMembFoundChangeInset.setMfchg_change_name(tdNtPromoCodeUpdate.getPromo_name());
//                tdNtMembFoundChangeInset.setMfchg_change_name("其它");
                tdNtMembFoundChangeInset.setMfchg_change_type(Constants.FundChangeType.wallet_in);
                tdNtMembFoundChangeInset.setState(Constants.PayState.success);
                tdNtMembFoundChangeInset.setType(Constants.ChargeType.other_code);
                tdNtMembFoundChangeInset.setMfchg_number(CommonUtil.getOrderNub());
                tdNtMembFoundChangeDao.insertTdNtMembFoundChange(tdNtMembFoundChangeInset);
                /**
                 * 更新余额
                 */
                TdNtMemberInvite tdNtMemberInvite = new TdNtMemberInvite();
                tdNtMemberInvite.setMemb_balance(tdNtMembFoundChangeInset.getMfchg_back_amount());
                tdNtMemberInvite.setMemb_id(memb_id);
                tdNtMemberDao.updateTdNtMemberBalance(tdNtMemberInvite);
                /**
                 * 更新优惠码信息
                 */
                tdNtPromoCodeUpdate.setMemb_id(memb_id);
                tdNtPromoCodeUpdate.setState(2);
                tdNtPromoCodeDao.updateTdNtPromoCodeUpdate(tdNtPromoCodeUpdate);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("恭喜您,领取成功!");
            } else { //查找企业码是否存在
                /**
                 * 查找企业码是否存在
                 */
                TdNtCooperation tdNtCooperation = tdNtCooperationDao.findCooperationCode(promo_code);
                if (tdNtCooperation != null ){
                    TdNtCooperationMember tdNtCooperationMember = new TdNtCooperationMember();
                    tdNtCooperationMember.setCpt_id(tdNtCooperation.getCpt_id());
                    tdNtCooperationMember.setMemb_id(memb_id);
                    //判断合作商用户关联表信息是否存在
                    TdNtCooperationMember findtdNtCooperationMember = tdNtCooperationDao.findTdNtCooperationMember(tdNtCooperationMember);
                    if (findtdNtCooperationMember == null) {
                        tdNtCooperationDao.updateTdNtCooperationMember(tdNtCooperationMember);
                    }
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip("企业码!");
                }
                else {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip("优惠码无效");
                }

            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
